// CommodityController.java (控制器层)
package com.ywb.controller;

import com.ywb.anno.Log;
import com.ywb.pojo.*;
import com.ywb.service.CommodityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/commodities")
@RestController
public class CommodityController {

    @Autowired
    private CommodityService commodityService;

    @GetMapping("/all")
    public Result list() {
        log.info("查询全部商品数据");
        List<Commodity> commodities = commodityService.findAll();
        return Result.success(commodities);
    }

    @GetMapping
    public Result page(CommodityQueryParam commodityQueryParam) {
        log.info("分页查询，参数:", commodityQueryParam);
        PageResult<Commodity> pageResult = commodityService.page(commodityQueryParam);
        return Result.success(pageResult);
    }

    @Log
    @DeleteMapping
    public Result delete( @RequestParam("id") Integer commodityId) {
        log.info("删除商品{}", commodityId);
        commodityService.deleteById(commodityId);
        return Result.success();
    }

    @Log
    @PostMapping
    public Result add(@RequestBody Commodity commodity) {
        log.info("新增商品{}", commodity);
        commodityService.add(commodity);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getinfo(@PathVariable("id") Integer commodityId) {
        log.info("根据ID查询商品{}", commodityId);
        Commodity commodity = commodityService.getById(commodityId);
        return Result.success(commodity);
    }

    @Log
    @PutMapping
    public Result update(@RequestBody Commodity commodity) {
        log.info("修改商品{}", commodity);
        commodityService.update(commodity);
        return Result.success();
    }
}